package com.shoubei.cashiedesk.serialPort;

import android.graphics.Bitmap;
import android.text.TextUtils;


import com.shoubei.cashiedesk.utils.SomeUtils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import android_serialport_api.SerialPort;

import static com.shoubei.cashiedesk.base.App.toast;


/**
 * @author
 * @version 创建时间：2018年11月15日 下午3:32:34 类说明 串口打印工具类
 */
public class SerialPostPrint {

	public static void serialPrintTest(String serialPath, int baudrate) {
		OutputStream out = null;
		SerialPort mSerialPort = null;
		try {

			mSerialPort = new SerialPort(new File(serialPath), baudrate, 0, true);
			out = mSerialPort.getOutputStream();
			out.write("打印测试\n\n\n\n\n\n".getBytes("gb2312"));
			byte[] qiezhiCmd = { 27, 105 };
			out.write(qiezhiCmd);
		} catch (Exception e) {
		} finally {
			if (out != null) {
				try {
					out.flush();
					out.close();
					out = null;
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
			if (mSerialPort != null) {
				mSerialPort.close();
				mSerialPort = null;
			}
		}
	}

	public static void serialPrint(PrintAndKexianDevice printDevice, String content, boolean isOpenCashbox, int spec, boolean isPrintLogo, String receiptUrl) {
		synchronized (SerialPostPrint.class) {
			SerialPort mSerialPort = null;
			OutputStream out = null;
			byte[] cmd = { 10, 10 };// 换行
			try {
				mSerialPort = new SerialPort(new File(printDevice.getDevice()), printDevice.getBaudrate(), 0, true);
				System.err.println("mSerialPort:" + mSerialPort);
				if (mSerialPort != null) {
					out = mSerialPort.getOutputStream();
					if (isPrintLogo) {
						// 打印logo
						Bitmap logoBitmap = LoadAdvertisingMSG.getInstance(null).getLogo();
						serialPrintImage(out, logoBitmap, 200);
						out.write(cmd);
					}
					// 正文
					out.write(content.getBytes("GB2312"));

					// 4两个换行
					out.write(cmd);
					out.write(cmd);
					// 切纸命令
					byte[] qiezhiCmd = { 27, 105 };
					out.write(qiezhiCmd);
					out.flush();
					out.close();
				}
			} catch (SecurityException e) {
				e.printStackTrace();
				toast("本地小票机打印失败:" + e.getMessage());
			} catch (Exception e) {
				e.printStackTrace();
				toast("本地小票机打印失败:" + e.getMessage());
			} finally {
				// 释放资源
				if (out != null) {
					try {
						out.flush();
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					out = null;
				}
				if (mSerialPort != null) {
					mSerialPort.close();
					mSerialPort = null;
				}

			}
		}
	}

	/**
	 * 打印图片
	 * 
	 * @param out
	 * @param bitmap
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	private static void serialPrintImage(OutputStream out, Bitmap bitmap, int width) throws UnsupportedEncodingException, IOException {
//		if (bitmap != null) {
//			out.write(PrinterCmdUtils.alignCenter().getBytes());
//			out.write(SomeUtils.getBitmapPrintByte(bitmap, width));
//			out.write(PrinterCmdUtils.alignLeft().getBytes());
//			if (bitmap != null && !bitmap.isRecycled()) {
//				bitmap.recycle();
//				bitmap = null;
//			}
//		}
	}

}
